﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using si.caha.Repository;
using si.caha.Model;

namespace si.caha.Repository
{
    public class OrderRepository : BaseRepository
    {
        public OrderRepository(DataContext dataConext) : base(dataConext){}

        public Order Get(int id)
        {
            return GetDataContext().Orders.FirstOrDefault(o => o.OrderId == id);
        }

        public List<Order> GetAll()
        {
            return GetDataContext().Orders.ToList();
        }
        
        public void ToggleCompleted(int id)
        {
            var order = Get(id);
            order.Completed = order.Completed ? false : true;
            Save(order);
        }
        
        public void Save(Order entity)
        {
            if (entity.OrderId == 0)
            {
                entity.OrderDate = System.DateTime.Now;
                entity.OrderCompletedDate = System.DateTime.Now;
                GetDataContext().Orders.Add(entity);
            }
            else
            {
                entity.OrderCompletedDate = System.DateTime.Now;
            }
            
            GetDataContext().SaveChanges();
        }
    }
}